package com.example.administrator.dc.Activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.example.administrator.dc.Bean.Carousel;
import com.example.administrator.dc.R;
import com.example.administrator.dc.fragment.ClubCourse;
import com.example.administrator.dc.fragment.ClubIntroduce;
import com.example.administrator.dc.fragment.ClubVideo;
import com.example.administrator.dc.fragment.HomeFragment;
import com.example.administrator.dc.fragment.TwoFragment;
import com.example.administrator.dc.model.CarouselModel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;

import static android.R.color.black;
import static com.example.administrator.dc.R.color.green;
import static com.example.administrator.dc.helper.Carousel.setCarousel;

public class ClubDetail extends BaseActivity {
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private ClubIntroduce clubIntroduce;
    private ClubCourse clubCourse;
    private ClubVideo clubVideo;
    private ViewPager vp;
    private ConvenientBanner convenient;//顶部广告栏控件
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private Context context;
    private List<String> images =new ArrayList<>();

    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_detail);
        Intent intent =getIntent();
        String a=String.valueOf(intent.getStringExtra("id"));
        int i = Integer.parseInt(a);
        setClubid(i);
        initView();
        init();
        initEvents();
        initFragment();




    }
    private void initView() {
       /* for(int i=0;i<4;i++){
           images.add(imgUrl+image[i]);
        }
        context=getActivity();
        li=(LinearLayout)view.findViewById(R.id.play);
        // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
        convenient = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
        Display d=getActivity().getWindowManager().getDefaultDisplay();
                *//*获取布局参数*//*
                i=li.getLayoutParams();
                i.height=d.getHeight()/3;
                i.width=d.getWidth();
                setCarousel(context,convenient,images,i.width,i.height);*/
        CarouselModel carouselModel=new  CarouselModel();
        Call<List<Carousel>> call1=carouselModel.Carousel();
        call1.enqueue(new Callback<List<Carousel>>() {
            @Override
            public void onResponse(Call<List<Carousel>> call1, retrofit2.Response<List<Carousel>> response)
            {
                List<Carousel> res=response.body();
                for (int i=0;i<res.size();i++)
                {
                    images.add(res.get(i).getPhoto());
                }
                context=getApplication();
                li=(LinearLayout)findViewById(R.id.play);
                // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
                convenient = (ConvenientBanner) findViewById(R.id.convenientBanner);
                Display d=getWindow().getWindowManager().getDefaultDisplay();
               /*获取布局参数*/
                i=li.getLayoutParams();
                i.height=d.getHeight()/3;
                i.width=d.getWidth();
                setCarousel(context,convenient,images,i.width,i.height);

            }
            @Override
            public void onFailure(Call<List<Carousel>> call, Throwable t) {

            }
        });

    }
    private  void init(){
        radioButton1= (RadioButton) findViewById(R.id.radioButton);
        radioButton2= (RadioButton) findViewById(R.id.radioButton2);
        radioButton3= (RadioButton) findViewById(R.id.radioButton3);
        //  radioButton3= (RadioButton) findViewById(R.id.radioButton3);
        vp= (ViewPager) findViewById(R.id.viewPager);


        fragmentManager=getSupportFragmentManager();      //getChildFragmentManager();在fragment

        fragmentTransaction=fragmentManager.beginTransaction();
        clubIntroduce=new ClubIntroduce();
        clubCourse=new ClubCourse();
        clubVideo=new ClubVideo();
        // threeFragment=new ThreeFragment();
        //  fourFragment=new FourFragment();

        fragmentTransaction.add(R.id.viewPager,clubIntroduce);
        fragmentTransaction.add(R.id.viewPager,clubCourse);
        fragmentTransaction.add(R.id.viewPager,clubVideo);
        //fragmentTransaction.add(R.id.viewPager,threeFragment);
        //fragmentTransaction.add(R.id.framelayout,fourFragment);

        fragmentTransaction.commit();

    }
    private  void initEvents(){

        radioButton1.setOnClickListener(onClickListener);
        radioButton2.setOnClickListener(onClickListener);
        radioButton3.setOnClickListener(onClickListener);
        //    radioButton3.setOnClickListener(onClickListener);


    }
    View.OnClickListener  onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.radioButton:
                   Toast.makeText(ClubDetail.this, "简介", Toast.LENGTH_SHORT).show();
                    selectTab(0);
                    break;
                case R.id.radioButton2:
                    Toast.makeText(ClubDetail.this, "课程", Toast.LENGTH_SHORT).show();
                    selectTab(1);
                    break;
                case R.id.radioButton3:
                    Toast.makeText(ClubDetail.this,"视频",Toast.LENGTH_SHORT).show();
                    selectTab(2);

            }



        }
    };
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                radioButton1.setTextColor(getResources().getColor(green));
                radioButton2.setTextColor(getResources().getColor(black));
                radioButton3.setTextColor(getResources().getColor(black));
                break;
            case 1:
                radioButton1.setTextColor(getResources().getColor(black));
                radioButton2.setTextColor(getResources().getColor(green));
                radioButton3.setTextColor(getResources().getColor(black));

                break;
            case 2:
                radioButton1.setTextColor(getResources().getColor(black));
                radioButton2.setTextColor(getResources().getColor(black));
                radioButton3.setTextColor(getResources().getColor(green));
        }
        vp.setCurrentItem(i);
    }
    private void initFragment(){
        listfragment=new ArrayList<Fragment>();
        listfragment.add(new ClubIntroduce());
        listfragment.add(new ClubCourse());
        listfragment.add(new ClubVideo());
        //    listfragment.add(new ThreeFragment());

        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }

            @Override
            public int getCount() {
                return listfragment.size();
            }
        };
        vp.setAdapter(fpadapter);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                System.out.println("----"+String.valueOf(position));
                int i = Integer.parseInt(String.valueOf(position));
                vp.setCurrentItem(position);
                if(i==0){

                    radioButton1.setTextColor(getResources().getColor(green));
                    radioButton2.setTextColor(getResources().getColor(black));
                    radioButton3.setTextColor(getResources().getColor(black));
                    fragmentManager=getSupportFragmentManager();
                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.show(clubIntroduce);
                    fragmentTransaction.hide(clubCourse);
                    fragmentTransaction.hide(clubVideo);
                    //    fragmentTransaction.hide(threeFragment);
                    fragmentTransaction.commit();



                }
                if(i==1){
                    radioButton1.setTextColor(getResources().getColor(black));
                    radioButton2.setTextColor(getResources().getColor(green));
                    radioButton3.setTextColor(getResources().getColor(black));
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.show(clubCourse);
                    fragmentTransaction.hide(clubIntroduce);
                    fragmentTransaction.hide(clubVideo);
                    //   fragmentTransaction.hide(threeFragment);
                    fragmentTransaction.commit();

                }
            if(i==2){
                radioButton1.setTextColor(getResources().getColor(black));
                radioButton2.setTextColor(getResources().getColor(black));
                radioButton3.setTextColor(getResources().getColor(green));

                    fragmentTransaction=fragmentManager.beginTransaction();
                    fragmentTransaction.show(clubVideo);
                    fragmentTransaction.hide(clubCourse);
                    fragmentTransaction.hide(clubIntroduce);
                    fragmentTransaction.commit();

                }

             /*   resetImage();
                resetText();
                selectTab(position);*/
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

}
